package main

import (
	"flag"
	"fmt"

	"gitee.com/thubcc/rtlmmap"
)

var (
	argPort = flag.Int("p", 0, "rtl sdr port")
	argPPM  = flag.Int("m", 1, "rtl sdr ppm")
	argSet  = flag.Bool("s", false, "set sdr ppm")
)

func main() {
	flag.Parse()
	fmt.Println("RTL Devices:", rtlmmap.GetDeviceCount())
	dev := rtlmmap.NewDevice(*argPort)
	dev.Open()
	dev.Dump()
	var err error
	data := make([]uint8, rtlmmap.EepromSize)
	if err = dev.ReadEeprom(data, 0, rtlmmap.EepromSize); err != nil {
		fmt.Println(err)
		return
	}
	for i := 0; i < rtlmmap.EepromSize; i++ {
		fmt.Printf("%x ", data[i])
		if i&0xf == 0xf {
			fmt.Printf("\n")
		}
	}
	hinfo, errs := dev.GetHwInfo()
	if errs != nil {
		fmt.Println(errs)
		return
	}
	if *argSet {
		hinfo.PPM = int8(*argPPM)
		fmt.Println(hinfo)
		rtlmmap.SetHwInfoPPM(hinfo, data)
		for i := 0; i < rtlmmap.EepromSize; i++ {
			fmt.Printf("%02x ", data[i])
			if i&0xf == 0xf {
				fmt.Printf("\n")
			}
		}
		err = dev.WriteEeprom(data[rtlmmap.PPMPos:], rtlmmap.PPMPos, 1)
		if err != nil {
			fmt.Println(err)
		}
	}
}
